
package com.sausageking.client.merchant.gin;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.inject.client.AbstractGinModule;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.inject.Inject;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
import com.sausageking.client.MerchantService;
import com.sausageking.client.MerchantServiceAsync;
import com.sausageking.client.merchant.mvp.AppPlaceFactory;
import com.sausageking.client.merchant.mvp.AppPlaceHistoryMapper;
import com.sausageking.client.merchant.place.MerchantSignupPlace;
import com.sausageking.client.merchant.ui.CodeView;
import com.sausageking.client.merchant.ui.CodeViewImpl;
import com.sausageking.client.merchant.ui.MerchantSignupView;
import com.sausageking.client.merchant.ui.MerchantSignupViewImpl;
import com.sausageking.client.merchant.ui.StickerView;
import com.sausageking.client.merchant.ui.StickerViewImpl;
import com.sausageking.client.mvp.AppActivityMapper;

public class MerchantGinModule extends AbstractGinModule {

  @Override
  protected void configure() {
    bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);
    bind(ActivityMapper.class).to(AppActivityMapper.class).in(Singleton.class);

    // bind the views
    bind(MerchantSignupView.class).to(MerchantSignupViewImpl.class);
    bind(StickerView.class).to(StickerViewImpl.class);
    bind(CodeView.class).to(CodeViewImpl.class);

    // bind places
    bind(Place.class).annotatedWith(Names.named("merchant_default")).to(
        MerchantSignupPlace.class);
  }

  @Inject
  @Singleton
  @Provides
  ActivityManager provideActivityManager(EventBus eventBus,
      ActivityMapper mapper) {
    return new ActivityManager(mapper, eventBus);
  }

  @Inject
  @Singleton
  @Provides
  AppPlaceHistoryMapper provideAppPlaceHistoryMapper(AppPlaceFactory factory) {
    AppPlaceHistoryMapper historyMapper = GWT
        .create(AppPlaceHistoryMapper.class);
    historyMapper.setFactory(factory);
    return historyMapper;
  }

  @SuppressWarnings("deprecation")
  @Inject
  @Singleton
  @Provides
  PlaceHistoryHandler providePlaceHistoryHandler(
      AppPlaceHistoryMapper historyMapper, PlaceController placeController,
      EventBus eventBus, @Named("merchant_default") Place defaultPlace) {
    PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
    historyHandler.register(placeController, eventBus, defaultPlace);
    return historyHandler;
  }

  @SuppressWarnings("deprecation")
  @Inject
  @Singleton
  @Provides
  PlaceController providePlaceController(EventBus eventBus) {
    return new PlaceController(eventBus);
  }

  @Inject
  @Provides
  MerchantServiceAsync provideMerchantService() {
    MerchantServiceAsync service = GWT.create(MerchantService.class);
    return service;
  }

}
